package com.biz.primus.ms.commodity.dao.redis;

import com.biz.primus.base.redis.CrudRedisDao;
import com.biz.primus.base.redis.util.RedisUtil;
import com.biz.primus.ms.commodity.dao.ro.ProductRo;
import com.biz.primus.ms.commodity.dao.ro.SaleTagRo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 销售标签 RedisDao
 *
 * @author david-liu
 * @date 2017年01月04日
 * @reviewer
 * @see
 */
@Repository
public class SaleTagRedisDao extends CrudRedisDao<SaleTagRo, Long> implements Serializable {

    private static final long serialVersionUID = 1010027829501008612L;

    /**
     * 根据商品ID获取商品的销售标签
     * @param productId 商品ID
     * @return 销售标签列表
     */
    public List<SaleTagRo> findProductSaleTags(Long productId) {
        String productSaleTagSortedSetKey = getProductSaleTagSortedSetKey(productId);
        Set<byte[]> saleTagIdByteSet = zRange(productSaleTagSortedSetKey, 0, -1);
        return findByIds(saleTagIdByteSet);
    }

    /**
     * 通过销售标签名称  获取 销售标签Id
     * @param tagName
     * @return
     */
    public Long getSaleTagIdByName(String tagName) {
        String saleTagNameIdMappingStringKey = this.getSaleTagNameIdMappingStringKey(tagName);
        byte[] bytes = super.get(saleTagNameIdMappingStringKey);
        return RedisUtil.byteArrayToLong(bytes);
    }

    /**
     * 设置销售标签名称与销售标签Id之间的mapping关系
     * @param tagName
     * @param tagId
     */
    public void setSaleTagNameIdMapping(String tagName, Long tagId) {
        if (StringUtils.isNotBlank(tagName) && tagId != null) {
            String saleTagNameIdMappingStringKey = this.getSaleTagNameIdMappingStringKey(tagName);
            super.set(saleTagNameIdMappingStringKey, RedisUtil.toByteArray(tagId));
        }
    }

    /**
     * 移除销售标签名称与Id对应关系
     * @param tagName
     */
    public void removeSaleTagNameIdMapping(String tagName) {
        if (StringUtils.isNotBlank(tagName)) {
            String saleTagNameIdMappingStringKey = this.getSaleTagNameIdMappingStringKey(tagName);
            super.del(saleTagNameIdMappingStringKey);
        }
    }

    /**
     * tagName 与 saleTagId mapping key
     * @param tagName
     * @return
     */
    private String getSaleTagNameIdMappingStringKey(String tagName) {
        return super.getKeyByParams("tagName", tagName);
    }

    private String getProductSaleTagSortedSetKey(Long productId) {
        return super.getKeyByParams("productId", productId);
    }

    /******************************以下为销售标签与商品关系的维护****************************/

    public String createProductSaleTagSortKey(Long saleTagId){
        return "sort:product:by:saletag:"+saleTagId;
    }

    public String createProductSaleTagCategorySortKey(Long saleTagId,Long categoryId){
        return "sort:product:by:saletag:"+saleTagId+":categoryId:"+categoryId;
    }

    public void addProductSort(Long saleTagId, ProductRo product, Double sort){
        String key = createProductSaleTagSortKey(saleTagId);
        long append = System.currentTimeMillis();
        long newSort = Long.valueOf(sort+""+append);
        zadd(key, newSort, String.valueOf(product.getProductCode()).getBytes());
        String key2 = createProductSaleTagCategorySortKey(saleTagId, product.getCategoryId());
        zadd(key2, newSort, String.valueOf(product.getProductCode()).getBytes());
    }

    public void remProductSort(Long saleTagId,ProductRo product){
        String key = createProductSaleTagSortKey(saleTagId);
        String key2 = createProductSaleTagCategorySortKey(saleTagId, product.getCategoryId());
        zrem(key, String.valueOf(product.getProductCode()).getBytes());
        zrem(key2, String.valueOf(product.getProductCode()).getBytes());
    }

    public List<String> getProductCodes(Long saleTagId, Long categoryId, int page, int pageSize){
        String key = categoryId!=null?createProductSaleTagCategorySortKey(saleTagId, categoryId):createProductSaleTagSortKey(saleTagId);
        Set<byte[]> productCodeset = zRange(key, page*pageSize, (page+1)*pageSize-1);
        List<String> result = new ArrayList<>();
        for(byte[] idbyt : productCodeset){
            result.add(new String(idbyt));
        }
        return result;
    }

}
